package com.code.leetcode.before50;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/7/22 23:01
 */
public class The48 {
    public void rotate(int[][] matrix) {
        int n = matrix.length;
        for (int i = 0; i < n / 2 + n % 2; i++) {
            for (int j = 0; j < n / 2; j++) {
                int[] currs = new int[4];
                int row = i;
                int col = j;
                for (int k = 0; k < 4; k++) {
                    currs[k] = matrix[row][col];
                    int x = row;
                    row = col;
                    col = n - 1 - x;
                }
                for (int k = 0; k < 4; k++) {
                    matrix[row][col] = currs[(k + 3) % 4];
                    int x = row;
                    row = col;
                    col = n - 1 - x;
                }
            }
        }
    }

    public void rotateBetter(int[][] matrix) {
        int len = matrix.length;
        for (int i = 0; i < (len / 2 + len % 2); i++) {
            for (int j = 0; j < len / 2; j++) {
                int tmp = matrix[len - j  - 1][i];
                matrix[len - j - 1][i] = matrix[len - 1 - i][len - j - 1];
                matrix[len - 1 - i][len - j - 1] = matrix[j][len - i - 1];
                matrix[j][len - i - 1] = matrix[i][j];
                matrix[i][j] = tmp;
            }
        }
    }

}
